
#import "WWSimpleString.h"

@implementation WWSimpleString

+ initialize { return [WWSimpleString setVersion:1], self; }

- init
{
  [super init];
  len = 0;
  maxLen = 32;
  str = (char *)NXZoneCalloc([self zone], 32, sizeof(char));
  return self;
}

- initWithStr:(const char *)newStr
{
  [self init];
  return [self setStr:newStr];
}

- free
{
  if (str) { NXZoneFree([self zone], str); }
  return [super free];
}


- setStr:(const char *)newStr
{
  if (newStr)
  { len = strlen(newStr);
    while ((len+1) > maxLen)
    {  maxLen *= 2;
       str = (char *)NXZoneRealloc([self zone], str, maxLen);
    }
    str = NXCopyStringBuffer(newStr);
  }
  else
  {  str = NULL;
     len = 0;
  }
  return self;
}

- (BOOL)sameAs:(const char *)aStr
{
  BOOL  ret;


  if (aStr)
  {  if (str) // passed in str isn't NULL; I'm not either
     {  if (!strcmp(aStr, str))
	{  ret = YES;
        }
        else
	{  ret = NO;
        }
     }
     else // passed in str isn't NULL; I am
     {  ret = NO;
     }
  }
  else
  {  if (str) // passed in str is NULL; I'm not
     {  ret = NO;
     }
     else // passed in str is NULL; I am too
     {  ret = YES;
     }
  }
  return ret;
}

- (const char *)str {  return (const char *)str; }

#define typeVector "*"
#define typeValues &str

- read:(NXTypedStream *)stream 
{
  int version;


  [super read:stream];

  version = NXTypedStreamClassVersion(stream, "WWSimpleString");
  if (version == 0) NXReadTypes(stream, "i", &version), version=1;
  if (version == 1)
  {  NXReadTypes(stream, typeVector, typeValues);
     if (str) 
     { len = strlen(str); 
       maxLen = len + 1;
     }     
     else
     {  str = (char *)NXZoneCalloc([self zone], 32, sizeof(char));
        len = 0;
        maxLen = 32;
     }     
  }
  return self;
}

- write:(NXTypedStream *)stream 
{
  [super write:stream];
  NXWriteTypes(stream, typeVector, typeValues);
  return self;
}


@end
